package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.MyselfState;

import java.util.HashMap;
import java.util.Map;

public class MyselfInfoActivity extends BaseActivity{
    ImageView mBack;
    TextView text1;
    TextView text2;
    TextView text3;

    LinearLayout layout1;
    LinearLayout layout2;
    LinearLayout layout3;
    MyselfState myselfState;
    Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myself_info);
        super.setTranslucent();

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mBack = (ImageView) findViewById(R.id.back_bt);

        text1= (TextView) findViewById(R.id.text1);
        text2= (TextView) findViewById(R.id.text2);
        text3= (TextView) findViewById(R.id.text3);

        Drawable drawable=getResources().getDrawable(R.drawable.xuanzhong_img); //获取图片
        drawable.setBounds(0, 0, (int)(16* Constant.getSCALE()), (int)(16* Constant.getSCALE()));  //设置图片参数
        text2.setCompoundDrawables(null,null,drawable,null);  //设置到哪个控件的位置（）

        layout1= (LinearLayout) findViewById(R.id.layout1);
        layout2= (LinearLayout) findViewById(R.id.layout2);
        layout3= (LinearLayout) findViewById(R.id.layout3);
        gson=new Gson();
    }

    private void initData() {
        getInfo();
    }

    private void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyselfInfoActivity.this.finish();
            }
        });
    }

    //GET 开发完成 http://118.89.185.176:8082/jdt/getAuthenticationList?userId={userId}

    private void getInfo() {
        String url= Constant.HOMEADDRESS + "jdt/getAuthenticationList?userId="+"1";
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub

                Log.d("reponse",response);
                if(response.length()>10) {
                    myselfState = gson.fromJson(response, new TypeToken<MyselfState>() {
                    }.getType());
                   for(int i=0;i<myselfState.getData().size();i++)
                   {
                       if(myselfState.getData().get(i).getAuthType().trim().equals("0"))
                       {
                           if(myselfState.getData().get(i).getAuthStatus().trim().equals("0"))
                           {
                               text1.setText("未填写");
                           }
                           else if(myselfState.getData().get(i).getAuthStatus().trim().equals("1"))
                           {
                               Drawable drawable=getResources().getDrawable(R.drawable.xuanzhong_img); //获取图片
                               drawable.setBounds(0, 0, (int)(16* Constant.getSCALE()), (int)(16* Constant.getSCALE()));  //设置图片参数
                               text1.setCompoundDrawables(null,null,drawable,null);  //设置到哪个控件的位置（）
                               text1.setText("");
                           }
                           final String authenticationId=myselfState.getData().get(i).getAuthenticationId();
                           layout1.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View view) {
                                   Intent intent=new Intent(MyselfInfoActivity.this,BaseInfoActivity.class);
                                   intent.putExtra("authenticationId",authenticationId);
                                   startActivity(intent);
                               }
                           });

                       }else if(myselfState.getData().get(i).getAuthType().trim().equals("1"))
                       {
                           if(myselfState.getData().get(i).getAuthStatus().trim().equals("0"))
                           {
                               text2.setText("未填写");
                           }
                           else if(myselfState.getData().get(i).getAuthStatus().trim().equals("1"))
                           {
                               Drawable drawable=getResources().getDrawable(R.drawable.xuanzhong_img); //获取图片
                               drawable.setBounds(0, 0, (int)(16* Constant.getSCALE()), (int)(16* Constant.getSCALE()));  //设置图片参数
                               text2.setCompoundDrawables(null,null,drawable,null);  //设置到哪个控件的位置（）
                               text2.setText("");
                           }
                           final String authenticationId=myselfState.getData().get(i).getAuthenticationId();
                           layout2.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View view) {
                                   Intent intent=new Intent(MyselfInfoActivity.this,ZhiYeActivity.class);
                                   intent.putExtra("authenticationId",authenticationId);
                                   startActivity(intent);
                               }
                           });


                       }else
                       {
                           if(myselfState.getData().get(i).getAuthStatus().trim().equals("0"))
                           {
                               text3.setText("未填写");
                           }
                           else if(myselfState.getData().get(i).getAuthStatus().trim().equals("1"))
                           {
                               Drawable drawable=getResources().getDrawable(R.drawable.xuanzhong_img); //获取图片
                               drawable.setBounds(0, 0, (int)(16* Constant.getSCALE()), (int)(16* Constant.getSCALE()));  //设置图片参数
                               text3.setCompoundDrawables(null,null,drawable,null);  //设置到哪个控件的位置（）
                               text3.setText("");
                           }

                           final String authenticationId=myselfState.getData().get(i).getAuthenticationId();
                           layout3.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View view) {
                                   Intent intent=new Intent(MyselfInfoActivity.this,RelateActivity.class);
                                   intent.putExtra("authenticationId",authenticationId);
                                   startActivity(intent);
                               }
                           });
                       }
                   }

                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(MyselfInfoActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }
        }) ;


        MyApplication.getQueue().add(stringRequest);

    }
}
